package com.jetinno.file

import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.lifecycle.lifecycleScope
import com.jetinno.simple.SimpleFragment
import com.jetinno.simple.utils.InputMethodUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * Created by zhangyuncai on 2023/9/21.
 */
class TxtFragment : SimpleFragment() {

    private val et_text_content: EditText by lazy { findViewById(R.id.et_text_content) }
    private val mSeekBar: SeekBar by lazy { findViewById(R.id.sb_txt) }
    private val mTxtScrollView: TxtScrollView by lazy { findViewById(R.id.sv_txt) }
    var isEdit=false
        set(value) {
            field = value
            //主动获取焦点
            et_text_content.isFocusable = true
            et_text_content.requestFocus()
            et_text_content.isFocusableInTouchMode = true
            et_text_content.performClick() //模拟用户点击来获取焦点
            InputMethodUtil.showSoftInput(mActivity, et_text_content)
        }

    override fun getLayoutId(): Int {
        return R.layout.fragment_txt
    }

    private fun showCommonCharDialog() {
        CommonCharDialog(mContext, et_text_content).show()
    }


    private val textActivity: TextActivity
        get() = mActivity as TextActivity


    override fun lazyFetchData() {

        lifecycleScope.launch(Dispatchers.IO){
            withContext(Dispatchers.Main){
                et_text_content.setText(textActivity.fileContent)
            }
        }

        et_text_content.setCustomSelectionActionModeCallback(object : ActionMode.Callback {
            override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
                val menuInflater = mode.menuInflater
                menuInflater.inflate(R.menu.customer_menu, menu)
                return true
            }

            override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
                return true
            }

            override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
                if (item.itemId == R.id.italic) {
                    showCommonCharDialog()
                    return false
                }
                return false
            }

            override fun onDestroyActionMode(mode: ActionMode) {
            }
        })
        mSeekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                if (fromUser) {
                    val height = mTxtScrollView.getChildAt(0).height
                    val y = (height * progress / 100L).toFloat()
                    mTxtScrollView.scrollTo(0, y.toInt())
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
            }
        })
        mTxtScrollView.setScrollListener(object : TxtScrollView.ScrollListener {
            override fun progress(progress: Int) {
                mSeekBar.setProgress(progress)
            }

        })
    }

    override fun initView() {
    }

    override fun initEvent() {
    }

    val editContent: String
        get() = et_text_content.text.toString()

    override fun onDestroyView() {
        InputMethodUtil.hideSoftInputFromWindow(mActivity, et_text_content)
        super.onDestroyView()
    }
}
